package 测试;

import java.util.Arrays;

public class 生成n位的格雷码 {

	public static void main(String[] args) {
		GrayCode gc = new GrayCode();
		for (int i = 1; i <= 5; i++) {
			System.out.println(Arrays.toString(gc.getGray(i)));
		}
	}

}

class GrayCode {
	public String[] getGray(int n) {
		if (n == 1)
			return new String[] { "0", "1" };
		else {
			String[] tmp = getGray(n - 1);
			String[] res = new String[tmp.length * 2];
			for (int i = 0; i < tmp.length; i++)
				res[i] = "0" + tmp[i];
			for (int i = 0; i < tmp.length; i++)
				res[i + tmp.length] = "1" + tmp[tmp.length - i - 1];
			return res;
		}
	}
}
